<template>
  <div>
    <a-modal
      v-model="organizationData.show"
      width="500px"
      style="overflow:hidden"
      @cancel="handleClose"
    >
      <div slot="title">
        <!-- <i class="el-icon-arrow-left"></i> -->
        <span>{{organizationData.title}}</span>
      </div>
      <el-form
        :model="parames"
        :rules="rules"
        label-position="left"
        ref="parames"
        label-width="70px"
        class="addForm"
        size="small"
      >
        <el-row :gutter="40" style="padding: 30px 30px">
          <el-col :span="24">
            <el-form-item label="名称:" prop="name">
              <el-input size="small" v-model="parames.name" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template slot="footer">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="handleSend">确定</el-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
export default {
  props: ["organizationData"],
  data() {
    return {
      parames: {
        name: "",
      },
      rules: {
        name: [
          { required: true, message: "必填", trigger: "blur" },
        ]
      },
    };
  },
  mounted() {},
  methods: {
    handleClose() {
      //关闭弹窗
      this.organizationData.show = false;
    },
    handleSend () { //提交信息
      this.$refs['parames'].validate((valid) => {
        if (valid) {
          if (this.organizationData.type == 'newRole') { //新建角色
            this.$post(this.$api.postProviderPersonelRoleAdd,{
              roleCode: '',//编号
              roleName: this.parames.name,//名称
            }).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          } else if (this.organizationData.type == 'newDepartment') { //新建子部门
            this.$post(this.$api.postProviderOrganizationAdd,{
              // seqNum: '',//部门编号
              label: this.parames.name,//部门名称
              parentId: this.organizationData.id,//上级部门ID
              companyId: this.organizationData.companyId,//服务商ID
            }).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          } else if (this.organizationData.type == 'editDepartment') { //修改部门
            this.$post(this.$api.postProviderOrganizationEdit,{
              id : this.organizationData.id,
              seqNum: this.organizationData.seqNum,//部门编号
              label: this.parames.name,//部门名称
              parentId: this.organizationData.parentId,//上级部门ID
              companyId: this.organizationData.companyId,//服务商ID
            }).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          } else if (this.organizationData.type == 'editRole') { //修改部门
            this.$post(this.$api.postProviderPersonelRoledit,{
              id : this.organizationData.id,
              roleCode: this.organizationData.roleCode,//部门编号
              roleName: this.parames.name,//部门名称
            }).then(response => {
              if (response.data.succeed) {
                this.$emit('checkRender')
                this.handleClose()
              }
            })
          }
        }
      })
    },
    render () {
      if (this.organizationData.type == 'editDepartment') { //修改部门
        this.parames.name = this.organizationData.label
      } else if (this.organizationData.type == 'editRole') {
        this.parames.name = this.organizationData.roleName
      }
    },
    resetData () {
      this.parames.name = ''
    }
  },
  computed: { //计算属性
    changeDialog () {
      return this.organizationData.show
    }
  },
  watch: {
    changeDialog: {
      handler (curVal, oldVal) {
        if (curVal) {
          this.render()
        } else {
          this.resetData()
        }
      }
    },
    deep: true
  }
}
</script>

<style scoped>
</style>
